<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/6/4
 * Time: 13:21
 * pv统计入库
 */
require_once dirname(dirname(__DIR__)) . "/common.php";

use app\lib\db\DbException;
use app\cls\tom\Queue;
use app\cls\tom\SysResource;
use app\cls\tom\Process;
use app\cls\tom\Tools;

$process = new Process(__FILE__);
$process->setProcessNum(1);
$process->initProcess();

$redis = SysResource::getRedis();
$db = SysResource::getDB(true);

exit();

try {
    while (true) {
        for ($i = 1; $i <= 5; $i++) {
            for ($j = 0; $j < 100; $j++) {
                $key = REDIS_PREFIX . "test_ugc_statics_" . $i;
                echo $key."\n";

                $dataStr = $redis->rPop($key);
                $result = json_decode($dataStr, true);
                if ($result) {
                    echo $dataStr."\n";
                    Tools::writeLog(["detail"=>$dataStr,"key"=>"处理UGC分享：".$material_key]);
                    // 查询分享总数
                    $db->select("log_id");
                    $db->from("data_logs_2018");
                    $db->where(['log_uniqueid' => $result['uniqueid'], "log_operate" => "share", "material_id" => $result['id']]);
                    $query = $db->get();
                    $datas1 = $query->all();
                    echo $db->last_query()."\n";
                    $count = count($datas1);
                    echo $count."\n";
                    //修改喜欢数
                    if(intval($count)>0){
                        $re = $db->update("user_material", ['count_material_share' => $count], ['video_uuid' => $result['uniqueid'], "id" => $result['id']]);
                        echo $re."\n";
                        echo $db->last_query()."\n";
                    }
                    exit;

                    // $db->select("count_material_share");
                    // $db->from("user_material");
                    // $db->where(['id' => $result['id'], 'video_uuid' => $result['uniqueid']]);
                    // $query = $db->get();
                    // $datas = $query->one();
                    // //如果分享数为0 或者分享数为空
                    // if (empty($datas['count_material_share']) || $datas['count_material_share'] == "0") {
                    //     //就去点赞表查询是否有这个数据被点赞了
                    //     $db->select("log_id");
                    //     $db->from("data_logs_2018");
                    //     $db->where(['log_uniqueid' => $result['uniqueid'], "log_operate" => "share", "material_id" => $result['id']]);
                    //     $query = $db->get();
                    //     //                        echo $db->last_query();exit;
                    //     $datas1 = $query->all();
                    //     $datas = count($datas1);
                    //     //修改喜欢数
                    //     if (empty($datas)) {
                    //         $re = $db->update("user_material", ['count_material_share' => "!count_material_share+1"], ['video_uuid' => $result['uniqueid'], "id" => $result['id']]);
                    //     } else {
                    //         $re = $db->update("user_material", ['count_material_share' => $datas + 1], ['video_uuid' => $result['uniqueid'], "id" => $result['id']]);
                    //     }
                    //
                    // } else {
                    //     //修改喜欢数
                    //     $re = $db->update("user_material", ['count_material_share' => "!count_material_share+1"], ['video_uuid' => $result['uniqueid'], "id" => $result['id']]);
                    // }
                } else {
                    continue;
                }
            }
        }
        echo '-----------------------------------';
        exit;
        sleep(3);
        $process->checkProcessTime(true);
    }
} catch (Exception $e) {
    $process->log($e->getMessage());
}
